home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / monitory / sysmon / src / showsys.c < prev    next >
C/C++ Source or Header  |  1995-11-04  |  11KB  |  384 lines

  1. /*
  2. **    $RCSfile: ShowSys.c,v $
  3. **    $Filename: ShowSys.c $
  4. **    $Revision: 0.8 $
  5. **    $Date: 1995/11/04 14:14:05 $
  6. **
  7. **    sysmon.library Task display command ShowSys (version 0.9)
  8. **    
  9. **    (C) Copyright 1995 by Etienne Vogt
  10. */
  11.  
  12. #include <exec/alerts.h>
  13. #include <exec/memory.h>
  14. #include <exec/execbase.h>
  15. #include <dos/dosextens.h>
  16. #include <dos/var.h>
  17. #include <dos/datetime.h>
  18. #include <workbench/startup.h>
  19. #include <devices/timer.h>
  20. #define __USE_SYSBASE
  21. #include <proto/exec.h>
  22. #include <proto/dos.h>
  23. #include <proto/utility.h>
  24. #include <proto/timer.h>
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <ctype.h>
  29. #include "sysmon.h"
  30. #include "sysmon_protos.h"
  31. #include "sysmon_pragmas.h"
  32.  
  33. #define TIBUFMAX    30
  34. #define TNAMEMAX    31
  35. #define NT_CLI    NT_USER
  36.  
  37. struct tiBuffer
  38. {    struct TaskInfo tib_tinfo;
  39.     char    tib_name[TNAMEMAX];
  40.     UBYTE    tib_type;
  41.     LONG    tib_clinum;
  42.     ULONG    tib_stacksize;
  43.     ULONG    tib_sigalloc;
  44.     ULONG    tib_sigwait;
  45.     ULONG    tib_sigrecvd;
  46.     ULONG    tib_sigexcept;
  47.     BYTE    tib_pri;
  48.     UBYTE    tib_flags;
  49.     UBYTE    tib_state;
  50. };
  51.     
  52. struct tiBufArray
  53. {    struct tiBufArray *tba_Link;
  54.     struct tiBuffer tba_bufs[TIBUFMAX];
  55. };
  56.  
  57. struct SysmonBase *SysmonBase;
  58. struct Library *TimerBase;
  59. struct timerequest myTimeReq;
  60. static struct RDArgs *myrda;
  61.  
  62. int main(void);
  63. static void cleanexit(ULONG rc);
  64. static char *uptimestr(struct EClockVal *clockval, ULONG clockrate);
  65. static char *cputimestr(struct EClockVal *clockval, ULONG clockrate);
  66. static char *taskstate(UBYTE state);
  67. static char *taskflags(UBYTE flags);
  68. static char *tasktype(UBYTE type, LONG clinum);
  69. static char *bstr2c(BSTR bstring);
  70. static char *cpustr(UWORD cpuflags);
  71. static char *filterctrl(char *string);
  72. APTR SPrintf(STRPTR buffer, STRPTR format, ...);
  73. void KPrintf(STRPTR format, ...);
  74. void freetibufs(struct tiBufArray *tibufs);
  75.  
  76. static UBYTE version[] = "$VER: ShowSys 0.9 (4.11.95)";
  77. static UBYTE template[] = "FULL/S";
  78.  
  79. #define    OPT_FULL    0
  80. #define OPTMAX        1
  81.  
  82. int main(void)
  83. {
  84.   ULONG EClockRate;
  85.   struct tiBufArray *tibufs;
  86.   LONG opts[OPTMAX];
  87.  
  88.   SysmonBase = NULL;
  89.   TimerBase = NULL;
  90.   myrda = NULL;
  91.  
  92.   if ((SysmonBase = (struct SysmonBase *)OpenLibrary("sysmon.library",0)) == NULL)
  93.   { if (DOSBase->dl_lib.lib_Version >= 36) Printf("ShowSys : Couldn't open sysmon.library.\n");
  94.     cleanexit(20);
  95.   }
  96.  
  97.   if (OpenDevice(TIMERNAME, UNIT_ECLOCK, (struct IORequest *)&myTimeReq, 0L))
  98.   { Alert(AT_Recovery|AG_OpenDev|AO_TimerDev);
  99.     cleanexit(100);
  100.   }
  101.   else
  102.   { char KickStart[8], NodeName[16];
  103.     struct EClockVal UpTime;
  104.     struct DateTime SysTime;
  105.     UBYTE strDate[LEN_DATSTRING], strTime[LEN_DATSTRING];
  106.  
  107.     memset((char *)opts, 0, sizeof(opts));
  108.     if ((myrda = ReadArgs(template, opts, NULL)) == NULL)
  109.     { PrintFault(IoErr(),"ShowSys");
  110.       cleanexit(20);
  111.     }
  112.  
  113.     TimerBase = (struct Library *)myTimeReq.tr_node.io_Device;
  114.     EClockRate = ReadEClock(&UpTime);
  115.     if (GetVar("KickStart", KickStart, sizeof(KickStart) - 1, GVF_GLOBAL_ONLY) > 0)
  116.       Printf("AmigaOS %s", KickStart);
  117.     else Printf("AmigaOS ?????");
  118.     if (GetVar("HostName", NodeName, sizeof(NodeName) - 1, GVF_GLOBAL_ONLY) > 0)
  119.       Printf(" at %s", NodeName);
  120.     else Printf("          ");
  121.     DateStamp(&SysTime.dat_Stamp);
  122.     SysTime.dat_Format = FORMAT_DOS;
  123.     SysTime.dat_Flags = 0;
  124.     SysTime.dat_StrDay = NULL;
  125.     SysTime.dat_StrDate = strDate;
  126.     SysTime.dat_StrTime = strTime;
  127.     if (DateToStr(&SysTime)) Printf("   %s %s", SysTime.dat_StrDate, SysTime.dat_StrTime);
  128.     else Printf("                    ");
  129.     Printf("   UpTime :%s\n", uptimestr(&UpTime, EClockRate));
  130.   }
  131.   
  132.   Printf("Address  Name                           State  Pri  CPU [%.5s]    Type\n",cpustr(SysBase->AttnFlags));
  133.   if (opts[OPT_FULL]) Printf(">  Stack  SigAlloc SigWait  SigRecvd SigExcpt  Flags      Dispatch\n");
  134.  
  135.   if ((tibufs = AllocVec(sizeof(struct tiBufArray), MEMF_PUBLIC | MEMF_CLEAR)) == NULL)
  136.   { Printf("ShowSys : No memory for TaskInfo buffers.\n");
  137.     cleanexit(20);
  138.   }
  139.   else
  140.   { struct TaskInfo *tinfo = NULL;
  141.     struct tiBufArray *tba = tibufs;
  142.     struct tiBuffer *buf;
  143.     int j, i = 0, numtask = 0;
  144.  
  145.     Forbid();
  146.     while (tinfo = smNextTaskInfo(tinfo))
  147.     { struct Task *task;
  148.       struct CommandLineInterface *cli;
  149.       char *comname;
  150.       int n;
  151.  
  152.       numtask++;
  153.       buf = &tba->tba_bufs[i];
  154.       memcpy(buf, tinfo, sizeof(struct TaskInfo));
  155.       task = tinfo->ti_Task;
  156.       if (task->tc_Node.ln_Name) strcpy(buf->tib_name, filterctrl(task->tc_Node.ln_Name));
  157.       else strcpy(buf->tib_name, "<< No Name >>");
  158.       buf->tib_flags = task->tc_Flags;
  159.       buf->tib_state = task->tc_State;
  160.       buf->tib_sigalloc = task->tc_SigAlloc;
  161.       buf->tib_sigwait = task->tc_SigWait;
  162.       buf->tib_sigrecvd = task->tc_SigRecvd;
  163.       buf->tib_sigexcept = task->tc_SigExcept;
  164.       if (buf->tib_state == TS_FROZENW) buf->tib_state = TS_FROZEN;
  165.       if (buf->tib_state == TS_WAIT && task->tc_SigWait == 0) buf->tib_state = TS_STOP;
  166.       buf->tib_pri = task->tc_Node.ln_Pri;
  167.       buf->tib_type = task->tc_Node.ln_Type;
  168.       if (buf->tib_type == NT_PROCESS && (cli = BADDR(((struct Process *)task)->pr_CLI)))
  169.       { buf->tib_clinum = ((struct Process *)task)->pr_TaskNum;
  170.     buf->tib_type = NT_CLI;
  171.     buf->tib_stacksize = cli->cli_DefaultStack * sizeof(ULONG);
  172.     if (cli->cli_Module && (n = strlen(buf->tib_name)) < TNAMEMAX - 4)
  173.     { comname = filterctrl(FilePart(bstr2c(cli->cli_CommandName)));
  174.       strcat(buf->tib_name, " (");
  175.       strncat(buf->tib_name, comname, TNAMEMAX - n - 4);
  176.       strcat(buf->tib_name, ")");
  177.     }
  178.       }
  179.       else if (buf->tib_type == NT_PROCESS) buf->tib_stacksize = ((struct Process *)task)->pr_StackSize;
  180.       else buf->tib_stacksize = (ULONG)task->tc_SPUpper - (ULONG)task->tc_SPLower;
  181.  
  182.       if (++i == TIBUFMAX)
  183.       { struct tiBufArray *tba1;
  184.  
  185.     if (tba1 = AllocVec(sizeof(struct tiBufArray), MEMF_PUBLIC | MEMF_CLEAR))
  186.     { tba->tba_Link = tba1;
  187.       tba = tba1;
  188.       i = 0;
  189.     }
  190.     else
  191.     { Permit();
  192.       freetibufs(tibufs);
  193.       Printf("ShowSys : No memory for TaskInfo buffers.\n");
  194.       cleanexit(20);
  195.     }
  196.       }
  197.     }
  198.     Permit();
  199.  
  200.     for (j = 0, tba = tibufs, i = 0 ; j < numtask ; j++)
  201.     { buf = &tba->tba_bufs[i];
  202.       Printf("%08lx %-30.30s %-5s %4ld %15s %-8s\n", buf->tib_tinfo.ti_Task,
  203.         buf->tib_name, taskstate(buf->tib_state), buf->tib_pri,
  204.         cputimestr(&buf->tib_tinfo.ti_CPUTime, EClockRate),
  205.         tasktype(buf->tib_type, buf->tib_clinum));
  206.       if (opts[OPT_FULL])
  207.       { if (buf->tib_state == TS_WAIT || buf->tib_state == TS_FROZENW)
  208.       Printf(">%7lu  %08lx %08lx %08lx %08lx  %8s %10lu\n", buf->tib_stacksize,
  209.         buf->tib_sigalloc, buf->tib_sigwait, buf->tib_sigrecvd, buf->tib_sigexcept,
  210.         taskflags(buf->tib_flags), buf->tib_tinfo.ti_DispCount);
  211.     else
  212.       Printf(">%7lu  %08lx -------- %08lx %08lx  %8s %10lu\n", buf->tib_stacksize,
  213.         buf->tib_sigalloc, buf->tib_sigrecvd, buf->tib_sigexcept, taskflags(buf->tib_flags),
  214.         buf->tib_tinfo.ti_DispCount);
  215.       }
  216.       if (CheckSignal(SIGBREAKF_CTRL_C))
  217.       { Printf("*** BREAK ***\n");
  218.     break;
  219.       }
  220.       if (++i == TIBUFMAX) 
  221.       { tba = tba->tba_Link;
  222.     i = 0;
  223.       }
  224.     }
  225.     freetibufs(tibufs);
  226.   }
  227.   cleanexit(0);
  228. }
  229.  
  230. static void cleanexit(ULONG rc)
  231. {
  232.   if (TimerBase) CloseDevice((struct IORequest *)&myTimeReq);
  233.   if (SysmonBase) CloseLibrary((struct Library *)SysmonBase);
  234.   if (myrda) FreeArgs(myrda);
  235.   exit(rc);
  236. }
  237.  
  238. static char *uptimestr(struct EClockVal *clockval, ULONG clockrate)
  239. { static char buffer[16];
  240.   ULONG seconds;
  241.   UWORD updays, uphours, upminutes, upseconds;
  242.  
  243.   seconds = (ULONG)((clockval->ev_hi * 4294967296.0 + clockval->ev_lo) / (double)clockrate);
  244.   updays = seconds / (24*60*60);
  245.   seconds %= 24*60*60;
  246.   uphours = seconds / (60*60);
  247.   seconds %= 60*60;
  248.   upminutes = seconds / 60;
  249.   upseconds = seconds % 60;
  250.   SPrintf(buffer, "%4lu %02lu:%02lu:%02lu", updays, uphours, upminutes, upseconds);
  251.   return buffer; 
  252. }
  253.  
  254. static char *cputimestr(struct EClockVal *clockval, ULONG clockrate)
  255. { static char buffer[18];
  256.   ULONG millis;
  257.   UWORD cpudays, cpuhours, cpuminutes, cpuseconds, cpumillis;
  258.  
  259.   millis = (ULONG)((clockval->ev_hi * 4294967296.0 + clockval->ev_lo) / (double)(clockrate / 1000));
  260.   cpudays = millis / (24*60*60*1000);
  261.   millis %= 24*60*60*1000;
  262.   cpuhours = millis / (60*60*1000);
  263.   millis %= 60*60*1000;
  264.   cpuminutes = millis / (60*1000);
  265.   millis %= 60*1000;
  266.   cpuseconds = millis / 1000;
  267.   cpumillis = millis % 1000;
  268.   SPrintf(buffer, "%2lu %02lu:%02lu:%02lu.%03lu", cpudays, cpuhours, cpuminutes, cpuseconds, cpumillis);
  269.   return buffer;
  270. }
  271.  
  272. static char *taskflags(UBYTE flags)
  273. { static char buffer[9];
  274.  
  275.   buffer[0] = (flags & TF_LAUNCH) ? 'l' : '-';
  276.   buffer[1] = (flags & TF_SWITCH) ? 's' : '-';
  277.   buffer[2] = (flags & TF_EXCEPT) ? 'e' : '-';
  278.   buffer[3] = (flags & TF_STACKCHK) ? 'c' : '-';
  279.   buffer[4] = (flags & TF_ETASK) ? 'x' : '-';
  280.   buffer[5] = (flags & (1L<<2)) ? '2' : '-';
  281.   buffer[6] = (flags & (1L<<1)) ? '1' : '-';
  282.   buffer[7] = (flags & TF_PROCTIME) ? 'p' : '-';
  283.   buffer[8] = '\0';
  284.   return buffer;
  285. }
  286.  
  287. static char *taskstate(UBYTE state)
  288. {
  289.   switch(state)
  290.   { case TS_INVALID:
  291.       return "INVLD";
  292.     case TS_ADDED:
  293.       return "ADDED";
  294.     case TS_RUN:
  295.       return "RUN";
  296.     case TS_READY:
  297.       return "READY";
  298.     case TS_WAIT:
  299.       return "WAIT";
  300.     case TS_EXCEPT:
  301.       return "EXCPT";
  302.     case TS_REMOVED:
  303.       return "REMVD";
  304.     case TS_STOP:
  305.       return "STOP";
  306.     case TS_FROZEN:
  307.       return "FROZN";
  308.     default:
  309.       return "?????";
  310.   }
  311. }
  312.  
  313. static char *tasktype(UBYTE type, LONG clinum)
  314. { static char buffer[10];
  315.  
  316.   switch(type)
  317.   { case NT_TASK:
  318.       SPrintf(buffer,"Task");
  319.       break;
  320.     case NT_PROCESS:
  321.       SPrintf(buffer,"Process");
  322.       break;
  323.     case NT_CLI:
  324.       SPrintf(buffer,"Cli %-4ld",clinum);
  325.       break;
  326.     default:
  327.       SPrintf(buffer,"???");
  328.       break;
  329.   }
  330.   return buffer;
  331. }
  332.  
  333. #define    AFB_68060    7
  334. #define AFF_68060    (1L<<7)
  335.  
  336. static char *cpustr(UWORD cpuflags)
  337. {
  338.   if (cpuflags & AFF_68060) return "68060";
  339.   else if (cpuflags & AFF_68040) return "68040";
  340.   else if (cpuflags & AFF_68030) return "68030";
  341.   else if (cpuflags & AFF_68020) return "68020";
  342.   else if (cpuflags & AFF_68010) return "68010";
  343.   else return "68000";
  344. }
  345.  
  346. static char *bstr2c(BSTR bstring)
  347. { static char buffer[256];
  348.   char *ptr;
  349.   int n;
  350.  
  351.   if ((ptr = BADDR(bstring)) == NULL || (n = *ptr++) == 0) return NULL;
  352.   strncpy(buffer, ptr, n + 1);
  353.   return buffer;
  354. }
  355.  
  356. static char *filterctrl(char *string)
  357. { static char buffer[TNAMEMAX];
  358.   int i;
  359.  
  360.   for (i = 0 ; *string && i < TNAMEMAX - 1 ; i++, string++)
  361.   { if ((*string & 0x7f) < ' ') buffer[i] = '.';
  362.     else buffer[i] = *string;
  363.   }
  364.   buffer[i] = 0;
  365.   return buffer;
  366. }
  367.  
  368. APTR SPrintf(STRPTR buffer, STRPTR format, ...)
  369. { return smVSPrintf(buffer, format, &format + 1);
  370. }
  371.  
  372. void KPrintf(STRPTR format, ...)
  373. { smVKPrintf(format, &format + 1);
  374. }
  375.  
  376. void freetibufs(struct tiBufArray *tibufs)
  377. { struct tiBufArray *nexttba;
  378.  
  379.   do
  380.   { nexttba = tibufs->tba_Link;
  381.     FreeVec(tibufs);
  382.   } while (tibufs = nexttba);
  383. }
  384.